package ie.wit.loanmanager.model;

import ie.wit.loanmanager.exceptions.UnknownItemException;
import ie.wit.loanmanager.model.item.FactoryItem;
import ie.wit.loanmanager.model.item.Item;

import java.util.Calendar;
import java.util.GregorianCalendar;


public class Loan {
	
	
	private int id;
	private String isWith;
	private String email;
	private String phone;
	private Calendar initialDate;
	private Calendar returnDate;
	private String descriptionLoan;
	private Item item;
	

	public Loan(String name, String email, String phone,
				int yearReturn, int monthReturn, int dayReturn, String descriptionLoan,
				String itemType, int _id) throws UnknownItemException {
		
			if (!name.equalsIgnoreCase("ME")) {
				this.isWith = name;
			} else {
				this.isWith = "ME";
			}
		
			this.email = email;
			this.phone = phone;
			this.initialDate = new GregorianCalendar();
			this.returnDate = new GregorianCalendar(yearReturn, monthReturn, dayReturn);
			this.descriptionLoan = descriptionLoan;
			
			FactoryItem factory = new FactoryItem();
			this.item = factory.getItem(itemType);
			
			this.id = _id;
	}
	
	

	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public String getIsWith() {
		return isWith;
	}



	public void setIsWith(String isWith) {
		this.isWith = isWith;
	}



	public String getEmail() {
		return email;
	}



	public void setEmail(String email) {
		this.email = email;
	}



	public String getPhone() {
		return phone;
	}



	public void setPhone(String phone) {
		this.phone = phone;
	}



	public String getInitialDate() {
		return initialDate.get(initialDate.YEAR) + "/" +
			   initialDate.get(initialDate.MONTH) + "/" +
			   initialDate.get(initialDate.DAY_OF_MONTH);
	}



	public void setInitialDate(Calendar initialDate) {
		this.initialDate = initialDate;
	}



	public String getReturnDate() {
		return returnDate.get(returnDate.YEAR) + "/" +
			   returnDate.get(returnDate.MONTH) + "/" +
			   returnDate.get(returnDate.DAY_OF_MONTH);
	}



	public void setReturnDate(Calendar returnDate) {
		this.returnDate = returnDate;
	}



	public String getDescriptionLoan() {
		return descriptionLoan;
	}



	public void setDescriptionLoan(String descriptionLoan) {
		this.descriptionLoan = descriptionLoan;
	}



	public Item getItem() {
		return item;
	}



	public void setItem(Item item) {
		this.item = item;
	}



	@Override
	public String toString() {
		
		if (getIsWith().equalsIgnoreCase("ME")) {
			return "\n\nID: " + getId() + " This item is with " + getIsWith() + " since " + getInitialDate() + "\n"
					+ "and is expected to be returned in " + getReturnDate() + " Type: " + getItem().toString() + "\n";
		}
		
		return "\n\nID: " + getId() + " This item is with " + getIsWith() + " since " + getInitialDate() + "\n"
				+ "and will be returned in " + getReturnDate() + 
				" Type: " + getItem().toString() + "\n";
	}



}
